package com.itbaizhan.paymentdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itbaizhan.paymentdemo.entity.OrderInfo;
import com.itbaizhan.paymentdemo.entity.RefundInfo;
import com.itbaizhan.paymentdemo.enums.OrderStatus;
import com.itbaizhan.paymentdemo.mapper.RefundInfoMapper;
import com.itbaizhan.paymentdemo.service.IOrderInfoService;
import com.itbaizhan.paymentdemo.service.IRefundInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itbaizhan
 * @since 04-26
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements IRefundInfoService {

    @Autowired
    IOrderInfoService iOrderInfoService;

    /**
     * 退款
     * @param orderNo  订单编号
     * @param reason  退款理由
     * @return
     */
    @Override
    public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
       //1,根据订单编号查询订单信息
        OrderInfo orderInfo = iOrderInfoService.findByOrderNo(orderNo);
        //根据订单编号生成退款单
        RefundInfo refundInfo = new RefundInfo();
        //3,订单编号
        refundInfo.setOrderNo(orderInfo.getOrderNo());
        //4,退款编号
        refundInfo.setRefundNo(String.valueOf(System.currentTimeMillis()));
        //5,原订单金额
        refundInfo.setTotalFee(orderInfo.getTotalFee());
        //6,退款金额
        refundInfo.setRefund(orderInfo.getTotalFee());
        //7,退款原因
        refundInfo.setReason(reason);
        baseMapper.insert(refundInfo);
        return refundInfo;
    }

    /**
     * 更新退款单
     * @param bodyString  微信返回的退款json数据
     *    在退款方法refund() ，得到bodyString
     */
    @Override
    public void updateRefund(String bodyString) {
        //json转为map
        Map resultMap = JSON.parseObject(bodyString, HashMap.class);

        //获取退款状态   从微信官网查看key
        String status = (String)resultMap.get("status");
        //微信支付退款单号
        String refundId = (String)resultMap.get("refund_id");
        //获取商户退款单号
        String refundNo = (String)resultMap.get("out_refund_no");

        //构建要更新的实体类
        LambdaUpdateWrapper<RefundInfo> refundInfoLambda = new LambdaUpdateWrapper<>();
        refundInfoLambda.set(RefundInfo::getRefundStatus,status);
        refundInfoLambda.set(RefundInfo::getRefundId,refundId);
        refundInfoLambda.set(RefundInfo::getContentReturn,bodyString); //返回的数据
        //根据退款单号进行退款
        refundInfoLambda.eq(RefundInfo::getRefundNo,refundNo);
        baseMapper.update(null,refundInfoLambda);


    }

    /**
     * 更新退款单状态  退款中  退款完成
     * @param bodyString
     */
    @Override
    public void updateRefundStatus(String bodyString) {
        //json转为map
        Map resultMap = JSON.parseObject(bodyString, HashMap.class);

        //获取退款状态   从微信官网查看key
        String status = (String)resultMap.get("refund_status");

        //获取商户退款单号
        String refundNo = (String)resultMap.get("out_refund_no");

        //构建要更新的实体类
        LambdaUpdateWrapper<RefundInfo> refundInfoLambda = new LambdaUpdateWrapper<>();
        refundInfoLambda.set(RefundInfo::getRefundStatus,status);

        //根据退款单号进行修改退款单状态
        refundInfoLambda.eq(RefundInfo::getRefundNo,refundNo);
        baseMapper.update(null,refundInfoLambda);


    }

    /**
     * 支付宝修改退款订单状态
     * @param orderNo
     */
    @Override
    public void updateRefundAliPayStatus(String orderNo) {
        LambdaUpdateWrapper<RefundInfo> refundInfoLambda = new LambdaUpdateWrapper<>();
        refundInfoLambda.set(RefundInfo::getRefundStatus, OrderStatus.REFUND_SUCCESS.getType());

        //根据退款单号进行修改退款单状态
        refundInfoLambda.eq(RefundInfo::getOrderNo,orderNo);
        baseMapper.update(null,refundInfoLambda);
    }
}
